package org.ricks.net.action.context.head;

import org.ricks.net.action.utils.CmdKit;

import java.nio.ByteBuffer;

public class DefComposeHeadMeta implements HeadMeta {
    private short cmd;
    private short subCmd;

    public DefComposeHeadMeta(short cmd, short subCmd) {
        this.cmd = cmd;
        this.subCmd = subCmd;
    }

    public DefComposeHeadMeta(short subCmd) {
        this.subCmd = subCmd;
    }

    @Override
    public int cmd() {
        return CmdKit.merge(cmd, subCmd);
    }

    @Override
    public ByteBuffer generateHeader(ByteBuffer buffer) {
        // 确保缓冲区有足够空间（4字节：cmd + subCmd）
        if (buffer == null || buffer.capacity() < 4) {
            buffer = ByteBuffer.allocate(4);
        }

        buffer.clear(); // 重置缓冲区
        buffer.putShort(cmd); // 写入主命令号
        buffer.putShort(subCmd); // 写入子命令号
        buffer.flip(); // 准备读取
        return buffer;
    }

    @Override
    public boolean isRpc() {
        return false; // 默认头部不是RPC
    }

    @Override
    public boolean isRpcResponse() {
        return false;
    }

    @Override
    public byte flags() {
        return 0; // 默认没有标志位
    }

    // 可选：添加getter/setter方法增强灵活性
    public short getCmd() {
        return cmd;
    }

    public void setCmd(short cmd) {
        this.cmd = cmd;
    }

    public short getSubCmd() {
        return subCmd;
    }

    public void setSubCmd(short subCmd) {
        this.subCmd = subCmd;
    }
}